﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Forbes.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;

namespace BOL.Linq.Distances
{
    public static partial class Similarity
    {
        /// <summary>
        ///                 n * a
        /// Forbes1 = -----------------
        ///           (a + b) * (a + c)
        /// </summary>
        public static double Forbes1(this IEnumerable<bool> first, IEnumerable<bool> second)
        {
            var bc = first.BinaryCardinality(second);

            return bc.Item1 * (bc.Item1 + bc.Item2 + bc.Item3 + bc.Item4) / (double)(bc.Item1 + bc.Item2) / (bc.Item1 + bc.Item3);
        }

        /// <summary>
        ///                    na - (a + b) * (a + c)
        /// Forbes2 = -----------------------------------------
        ///           n * min(a + b, a + c) - (a + b) * (a + c)
        /// </summary>
        public static double Forbes2(this IEnumerable<bool> first, IEnumerable<bool> second)
        {
            var bc = first.BinaryCardinality(second);

            var ab = bc.Item1 + bc.Item2;
            var ac = bc.Item1 + bc.Item3;
            var abac = ab * ac;
            var n = bc.Item1 + bc.Item2 + bc.Item3 + bc.Item4;

            return (n * bc.Item1 - abac) / (double)(n * Math.Min(ab, ac) - abac);
        }
    }
}
